package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.BusinessResultMapper;
import org.linlinjava.litemall.db.domain.BusinessProject;
import org.linlinjava.litemall.db.domain.BusinessResult;
import org.linlinjava.litemall.db.domain.BusinessResultExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Daisen.Z
 * @Date: 2021/6/3 22:01
 * @Version: 1.0
 * @Description:
 */
@Service
public class BusinessResultService {

    @Resource
    private BusinessResultMapper resultMapper;

    // 根据项目id模糊查询项目相关成果
    public List<BusinessResult> selectByProjectId(String projectId) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();
        criteria.andResWithProjectIdLike("%"+projectId+"%");
        List<BusinessResult> businessResults = resultMapper.selectByExample(example);
        return businessResults;
    }

    public int deleteByProjectId(String projectId) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();
        criteria.andResWithProjectIdEqualTo(projectId);
        return resultMapper.deleteByExample(example);
    }

    // 根据模块id和模块名称查询已审核通过结果
    public List<BusinessResult> selectByProjectIdAndModelId(String projectId, Integer modelId, Integer statusAudit) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();
        criteria.andResWithProjectIdLike("%"+projectId+"%");
        criteria.andOwningModuleEqualTo(modelId);
        criteria.andStatusAuditEqualTo(statusAudit);
        return resultMapper.selectByExample(example);
    }

    public int updateByPrimaryKeySelective(BusinessResult businessResult) {
        return resultMapper.updateByPrimaryKeySelective(businessResult);
    }

    public List<BusinessResult> querySelectiveByPojectIds(String peopleName, List<String> projectIds, BusinessResult businessResultParam, Integer page, Integer size, String sort, String order) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();
        BusinessResultExample.Criteria orCriteria = example.createCriteria();

        // 设置查询参数
        criteria.andOwningModuleEqualTo(businessResultParam.getOwningModule()); // 设置模块id

        if (businessResultParam != null){
            if (!StringUtils.isEmpty(businessResultParam.getResYear())){
                criteria.andResYearEqualTo(businessResultParam.getResYear());
                orCriteria.andResYearEqualTo(businessResultParam.getResYear());
            }
            if (!StringUtils.isEmpty(businessResultParam.getResMark())){
                criteria.andResMarkEqualTo("%"+businessResultParam.getResMark()+"%");
                orCriteria.andResMarkEqualTo("%"+businessResultParam.getResMark()+"%");
            }
            if (!StringUtils.isEmpty(businessResultParam.getResName())){
                criteria.andResWithProjectNameLike("%"+businessResultParam.getResWithProjectName()+"%");
                orCriteria.andResWithProjectNameLike("%"+businessResultParam.getResWithProjectName()+"%");
            }
            if (!StringUtils.isEmpty(businessResultParam.getDutyPersonalName())){
                criteria.andDutyPersonalNameLike("%"+businessResultParam.getDutyPersonalName()+"%");
                orCriteria.andDutyPersonalNameLike("%"+businessResultParam.getDutyPersonalName()+"%");
            }
            if (businessResultParam.getStatusAudit() != null){
                criteria.andStatusAuditEqualTo(businessResultParam.getStatusAudit());
                orCriteria.andStatusAuditEqualTo(businessResultParam.getStatusAudit());
            }
        }

        // 查找自己参与的项目
        if (projectIds != null){
            criteria.andResIdIn(projectIds);
        }

        // 设置限制参数
        // 或者录入人是自己也可以
        if (!StringUtils.isEmpty(peopleName)){
            orCriteria.andAdminNameEqualTo(peopleName);
            example.or(orCriteria);
        }

        //if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        //    example.setOrderByClause(sort + " " + order);
        //}


        PageHelper.startPage(page, size);
        return resultMapper.selectByExample(example);
    }

    public List<BusinessResult> querySelective(BusinessResult businessResultParam, Integer page, Integer size, String sort, String order) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();

        // 设置查询参数
        criteria.andOwningModuleEqualTo(businessResultParam.getOwningModule()); // 设置模块id
        if (businessResultParam != null){
            if (!StringUtils.isEmpty(businessResultParam.getResYear())){
                criteria.andResYearEqualTo(businessResultParam.getResYear());
            }
            if (!StringUtils.isEmpty(businessResultParam.getResMark())){
                criteria.andResMarkEqualTo("%"+businessResultParam.getResMark()+"%");
            }
            if (!StringUtils.isEmpty(businessResultParam.getResName())){
                criteria.andResWithProjectNameLike("%"+businessResultParam.getResWithProjectName()+"%");
            }
            if (!StringUtils.isEmpty(businessResultParam.getDutyPersonalName())){
                criteria.andDutyPersonalNameLike("%"+businessResultParam.getDutyPersonalName()+"%");
            }
            if (businessResultParam.getStatusAudit() != null){
                criteria.andStatusAuditEqualTo(businessResultParam.getStatusAudit());
            }
        }

        //if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
        //    example.setOrderByClause(sort + " " + order);
        //}

        PageHelper.startPage(page, size);
        return resultMapper.selectByExample(example);
    }

    public int insert(BusinessResult businessResult) {
        return resultMapper.insert(businessResult);
    }

    public int deleteByPrimaryKey(String resId) {
        return resultMapper.deleteByPrimaryKey(resId);
    }

    public BusinessResult selectByPrimaryKey(String resultId) {
        return resultMapper.selectByPrimaryKey(resultId);
    }

    public int update(BusinessResult businessResult) {
        return resultMapper.updateByPrimaryKeySelective(businessResult);
    }

    public List<BusinessResult> selectByProjectIdAndAuditStatus(String projectId, Integer statusAudit) {
        BusinessResultExample example = new BusinessResultExample();
        BusinessResultExample.Criteria criteria = example.createCriteria();
        criteria.andResWithProjectIdLike("%"+projectId+"%");
        criteria.andStatusAuditEqualTo(statusAudit);
        return resultMapper.selectByExample(example);    }
}
